//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_recycle_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type RecycleItemInfoDbItem struct {
	RecycleItemId   string `bson:"_id"`
	ProjectId       string `bson:"projectId"`
	RecycleItemType uint32 `bson:"recycleItemType"`
	RemoveUserId    string `bson:"removeUserId"`
	RemoveTime      int64  `bson:"removeTime"`
}

func (item *RecycleItemInfoDbItem) ToRecycleItemInfo() *project_recycle_api.RecycleItemInfo {
	return &project_recycle_api.RecycleItemInfo{
		RecycleItemId:   item.RecycleItemId,
		RecycleItemType: project_recycle_api.RECYCLE_ITEM_TYPE(item.RecycleItemType),
		RemoveUserId:    item.RemoveUserId,
		RemoveTime:      item.RemoveTime,
	}
}

type _RecycleDao struct {
	mc *mongo.Collection
}

func newRecycleDao(db *mongo.Database) (*_RecycleDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT + "recycle")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}, {"removeTime", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_RecycleDao{mc: mc}, nil
}

func (db *_RecycleDao) Insert(ctx context.Context, dbItem *RecycleItemInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_RecycleDao) Exist(ctx context.Context, projectId, recycleItemId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"projectId": projectId, "_id": recycleItemId})
	return count > 0, err
}

func (db *_RecycleDao) Get(ctx context.Context, projectId, recycleItemId string) (*RecycleItemInfoDbItem, error) {
	retItem := &RecycleItemInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"projectId": projectId, "_id": recycleItemId}).Decode(retItem)
	return retItem, err
}

func (db *_RecycleDao) Remove(ctx context.Context, projectId, recycleItemId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"projectId": projectId, "_id": recycleItemId})
	return err
}

func (db *_RecycleDao) Count(ctx context.Context, projectId string, filterByItemType bool, itemType uint32) (uint32, error) {
	query := bson.M{"projectId": projectId}
	if filterByItemType {
		query["recycleItemType"] = itemType
	}
	count, err := db.mc.CountDocuments(ctx, query)
	return uint32(count), err
}

func (db *_RecycleDao) List(ctx context.Context, projectId string, filterByItemType bool, itemType uint32, offset, limit int64) ([]*RecycleItemInfoDbItem, error) {
	query := bson.M{"projectId": projectId}
	if filterByItemType {
		query["recycleItemType"] = itemType
	}

	cursor, err := db.mc.Find(ctx, query, options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"removeTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*RecycleItemInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_RecycleDao) ListByType(ctx context.Context, projectId string, itemTypeList []uint32) ([]*RecycleItemInfoDbItem, error) {
	query := bson.M{"projectId": projectId, "recycleItemType": bson.M{"$in": itemTypeList}}

	cursor, err := db.mc.Find(ctx, query)
	if err != nil {
		return nil, err
	}
	retList := []*RecycleItemInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

// 清除所有信息面板的记录
// RECYCLE_ITEM_BOARD = 13;
func (db *_RecycleDao) RemoveAllBoard(ctx context.Context) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"recycleItemType": 13})
	return err
}

// 清除所有数据标注的记录
// RECYCLE_ITEM_DATA_ANNO = 16
func (db *_RecycleDao) RemoveAllDataAnno(ctx context.Context) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"recycleItemType": 16})
	return err
}

// 清除所有文件的记录
// RECYCLE_ITEM_FILE = 14
func (db *_RecycleDao) RemoveAllFile(ctx context.Context) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"recycleItemType": 14})
	return err
}
